 
function toJSON(arr) {
    var res = [];
    var isCollection = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
    	if (typeof value == "function") continue;
        if(typeof value == "object") { 
            if(isCollection) res.push(toJSON(value));
            else res[key] = toJSON(value); 
        } else {
            var str = "";
            if(!isCollection) str = '"' + key + '":';
            if(typeof value == "number") str += value;
            else if(value === false) str += 'false'; 
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; 
            res.push(str);
        }
    }
    var json = res.join(",");
    
    if(isCollection) return '[' + json + ']';
    return '{' + json + '}';
}
